home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / grafix / boards / a2410src.lha / Public / Source / demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-28  |  12.0 KB  |  472 lines

  1. /* 
  2.     Demoprogram showing random objects drawn by the 
  3.     graphics.library or/and my own gfx.library.
  4.  
  5.     name : demo.c
  6.     path : daten:c_src/a2410
  7.     author : jürgen schober
  8.     date : 4.2.94 taken from the dev_con demo files 
  9.     last change : 22.10.94
  10.  
  11.     Bugs : do not exactly know. But did never try AreaEllipse() in 
  12.            amiga mode. I guess a temp_rast should be allocated.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <a2410/typedefs.h>
  18. #include <a2410/devtiga.h>
  19. #include <a2410/ti_function_nums.h> 
  20. #include <exec/types.h>
  21. #include <dos/dos.h>
  22. #include <exec/memory.h>
  23. #include <graphics/gfx.h>
  24. #include <intuition/intuition.h>
  25. #include <math.h>
  26. #include <graphics/gfxmacros.h>
  27.  
  28. #include <clib/graphics_protos.h>
  29. #include <clib/intuition_protos.h>
  30. #include <clib/alib_protos.h>
  31.  
  32. #include <pragmas/exec_pragmas.h>
  33. #include <pragmas/graphics_pragmas.h>
  34. #include <pragmas/intuition_pragmas.h>
  35.  
  36. #include "tiga_macros.h"
  37. #include "gfx_pragmas.h"
  38. #include "graphics.h"
  39.  
  40. #define GSP_GFX   1
  41. #define AMIGA_GFX 2
  42.  
  43. #define PATTERN_B 1 
  44. #define FILL_B    2
  45. #define PATTERN_F (1<<PATTERN_B)
  46. #define FILL_F    (1<<FILL_B)
  47. #define NOFILL    0
  48. #define NOPATTERN 0
  49.  
  50. #define LIN (1<<0)
  51. #define BOX (1<<1)
  52. #define STR (1<<2)
  53. #define ELL (1<<3)
  54.  
  55. struct Library *IntuitionBase,*GfxBase=NULL;
  56. struct Library *GraphicsBase,*DiskfontBase;
  57. struct Window *win;
  58. struct Task *BoxTask = NULL, *TextTask = NULL, *EllipseTask = NULL, *LineTask = NULL;
  59. struct RastPort *RP;
  60. USHORT class;    /* Intu event class */
  61. USHORT code;    /* Intu event code */
  62. struct IntuiMessage *message;
  63. CONFIG    config;
  64. LONG gfx_mode=AMIGA_GFX,demo_mode=0L,fill_mode=NOFILL;
  65. BOOL  out = FALSE, line_busy = FALSE, box_busy = FALSE, txt_busy = FALSE, ell_busy = FALSE;
  66.  
  67.  
  68. /* =================================================================================== */
  69.  
  70. int event(void);
  71. void t_Task(void);
  72. void LinesDemo(void);
  73. void BoxDemo(void);
  74. void TextDemo(void);
  75. void CleanUp(void);
  76. void TestText(void);
  77. void EllipseDemo(void);
  78.  
  79. void main(int argc,char *argv[])
  80. {
  81.     int i=0;
  82.     ULONG sig,bits;
  83.     POINT pt[]={{100,100},{1000,500},{300,700},{1000,1000},{100,800},{50,500}};
  84.     WORD *area;
  85.     UBYTE strg[]="Hallo, Tiga scrolling Demo,....";
  86.  
  87.     onbreak(CleanUp);
  88.  
  89.     if (strncmp(argv[1],"?",1)==0) {
  90.         puts("demo of gsp_gfx.library, (c)94 - js -");
  91.         puts("usage : demo [TIGa] [PATtern] [BOX/LINe/TEXt/ELLipse/AREaellipse]");
  92.         puts("tiga  : use Tiga - default = amiga");
  93.         puts("pattern : use pattern    - default = nopattern");
  94.         puts("box     : box/lines-demo - default = lines");
  95.         exit(0);
  96.     }
  97.     
  98.     for (i=1;i<=argc;i++)  {
  99.         if (strncmp(argv[i],"tig",3)==0) gfx_mode=GSP_GFX;
  100.         if (strncmp(argv[i],"ami",3)==0) gfx_mode=AMIGA_GFX;
  101.         if (strncmp(argv[i],"box",3)==0) demo_mode|=BOX;
  102.         if (strncmp(argv[i],"tex",3)==0) demo_mode|=STR;
  103.         if (strncmp(argv[i],"lin",3)==0) demo_mode|=LIN;
  104.         if (strncmp(argv[i],"ell",3)==0) { demo_mode|=ELL; fill_mode=NOFILL; }
  105.         if (strncmp(argv[i],"are",3)==0) { demo_mode|=ELL; fill_mode|=FILL_F; }
  106.         if (strncmp(argv[i],"pat",3)==0) fill_mode|=PATTERN_F;
  107.     }
  108.     if (!demo_mode) demo_mode = LIN;
  109.  
  110.  
  111.     if (gfx_mode != AMIGA_GFX)
  112.         if ((GraphicsBase=(struct Library *)OpenLibrary("gfx.library",0L))==NULL)
  113.         {
  114.             puts("could not open gfx.library ...");
  115.             return;
  116.         }
  117.     if ((DiskfontBase=(struct Library *)OpenLibrary("diskfont.library",0L))==NULL)
  118.     {
  119.         puts("could not open diskfont.library ...");
  120.         return;
  121.     }
  122.     if ((GfxBase=(struct Library *)OpenLibrary("graphics.library",0L))==NULL)
  123.     {
  124.         puts("could not open graphics.library ...");
  125.         return;
  126.     }
  127.     if ((IntuitionBase=(struct Library *)OpenLibrary("intuition.library",0L))==NULL)
  128.     {
  129.          puts("could not open intuition.library ...");
  130.          return;
  131.     }
  132.     win=(struct Window *)OpenWindowTags(NULL,
  133.                         WA_Title,"My GfxDemo",
  134. //                        WA_Backdrop,TRUE,
  135.                         WA_AutoAdjust,TRUE,
  136.                         WA_Left,50,
  137.                         WA_Top,50,
  138.                         WA_Width,640,
  139.                         WA_Height,480,
  140.                         WA_MaxWidth,2000,
  141.                         WA_MaxHeight,2000,
  142.                         WA_MinWidth,10,
  143.                         WA_MinHeight,10,
  144.                         WA_DepthGadget,TRUE,
  145.                         WA_CloseGadget,TRUE,
  146.                         WA_SizeGadget,TRUE,
  147.                         WA_DragBar,TRUE,
  148.                         WA_IDCMP,IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE,
  149.                         WA_GimmeZeroZero,TRUE,
  150.                         TAG_DONE);
  151.  
  152.     RP=win->RPort;
  153.  
  154.     chkabort();
  155.     if (demo_mode & BOX)
  156.     {
  157.         BoxTask = CreateTask("DemoBoxTask",0,BoxDemo,20000);
  158.     }
  159.     if (demo_mode & STR)
  160.     {
  161.         TextTask = CreateTask("DemoTextTask",0,TextDemo,20000);
  162.     }
  163.     if (demo_mode & ELL)
  164.     {
  165.         EllipseTask = CreateTask("DemoEllipseTask",0,EllipseDemo,20000);
  166.     }
  167.     if (demo_mode & LIN)
  168.     {
  169.         LineTask = CreateTask("DemoLinesTask",0,LinesDemo,20000);
  170.     }
  171.     sig = (1L<<win->UserPort->mp_SigBit);
  172.     while (!out)
  173.     {
  174.         bits = Wait(sig | SIGBREAKF_CTRL_C);
  175.         chkabort();
  176.         if (bits & sig)
  177.            {
  178.             if (message = (struct IntuiMessage *)GetMsg(win->UserPort))
  179.                {
  180.                 class = message->Class;
  181.                 code  = message->Code;
  182.                 ReplyMsg((struct Message *)message);
  183.                 switch(class)
  184.                 {
  185.                        case CLOSEWINDOW:
  186.                         out = TRUE;
  187.                         break;
  188.                     case NEWSIZE:
  189.                         Forbid();
  190.                         config.mode.disp_hres=(win->Width);
  191.                         config.mode.disp_vres=(win->Height);
  192.                         config.mode.palet_size=(2<<(win->RPort->BitMap->Depth));
  193.                         Permit();
  194.                         break;
  195.                    default :
  196.                         break;
  197.                }
  198.                }
  199.         }
  200.     }
  201.     CleanUp();
  202. }
  203.  
  204. void __saveds CleanUp(void)
  205. {
  206.     out = TRUE;             // make sure tasks are Waiting on a CTRL-C
  207.     while ((line_busy) || (box_busy) || (txt_busy) || (ell_busy)) ;
  208.  
  209.     Forbid();
  210.  
  211.     if (BoxTask)     DeleteTask(BoxTask);
  212.     if (TextTask)    DeleteTask(TextTask);
  213.     if (EllipseTask) DeleteTask(EllipseTask);
  214.     if (LineTask)    DeleteTask(LineTask);
  215.  
  216.     Permit();
  217.  
  218.     SetDrPt(RP,0xFFFF);
  219.     RP->Flags|=(AREAOUTLINE);  // Outline löschen
  220.     if (win)           CloseWindow(win);
  221.     if (GfxBase)       CloseLibrary(GfxBase);
  222.     if (GraphicsBase)  CloseLibrary(GraphicsBase);
  223.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  224.     if (DiskfontBase)  CloseLibrary(DiskfontBase);
  225.  
  226.     exit(0);
  227. }
  228.  
  229. void __saveds LinesDemo()
  230. {
  231.     int x1,y1,x2,y2;
  232.     int x1v,y1v,x2v,y2v;
  233.     int i,c;
  234.     struct RastPort *my_rport;
  235.     
  236.     my_rport=win->RPort;
  237.     config.mode.disp_hres=(win->Width);
  238.     config.mode.disp_vres=(win->Height);
  239.     config.mode.palet_size=(2<<(win->RPort->BitMap->Depth));
  240.  
  241.     c = 0;
  242.     x1v = y1v = x2v = y2v = 0;
  243.     x1 = x2 = config.mode.disp_hres/2;
  244.     y1 = y2 = config.mode.disp_vres/2;
  245.  
  246.     if (fill_mode&PATTERN_F) 
  247.         SetDrPt(my_rport,0xf0f0);
  248.  
  249.     line_busy = TRUE;
  250.     do
  251.     {
  252.  
  253.         c = ((c)%(config.mode.palet_size-1))+1;
  254.     
  255.         SetAPen(my_rport,c);
  256.  
  257.         x1v = x1v/2 + (rand() & 15) - 7;
  258.         y1v = y1v/2 + (rand() & 15) - 7;
  259.         x2v = x2v/2 + (rand() & 15) - 7;
  260.         y2v = y2v/2 + (rand() & 15) - 7;
  261.  
  262.         i = 100;
  263.  
  264.         while(i--)
  265.         {
  266.             x1 += x1v;
  267.             y1 += y1v;
  268.             x2 += x2v;
  269.             y2 += y2v;
  270.  
  271.             if (x1 < 0)
  272.                 {
  273.                 x1 = - x1;
  274.                 x1v = - x1v;
  275.                 y1v += (rand() % 5) - 2;
  276.                 }
  277.  
  278.             if (y1 < 0)
  279.                 {
  280.                 y1 = - y1;
  281.                 y1v = - y1v;
  282.                 x1v += (rand() % 5) - 2;
  283.                 }
  284.  
  285.             if (x1 >= config.mode.disp_hres)
  286.                 {
  287.                 x1 = 2*config.mode.disp_hres - x1 - 2;
  288.                 x1v = - x1v;
  289.                 y1v += (rand() % 5) - 2;
  290.                 }
  291.  
  292.             if (y1 >= config.mode.disp_vres)
  293.                 {
  294.                 y1 = 2*config.mode.disp_vres - y1 - 2;
  295.                 y1v = -y1v;
  296.                 x1v += (rand() % 5) - 2;
  297.                 }
  298.  
  299.             if (x2 < 0)
  300.                 {
  301.                 x2 = - x2;
  302.                 x2v = - x2v;
  303.                 y2v += (rand() % 5) - 2;
  304.                 }
  305.  
  306.             if (y2 < 0)
  307.                 {
  308.                 y2 = - y2;
  309.                 y2v = - y2v;
  310.                 x2v += (rand() % 5) - 2;
  311.                 }
  312.  
  313.             if (x2 >= config.mode.disp_hres)
  314.                 {
  315.                 x2 = 2*config.mode.disp_hres - x2 - 2;
  316.                 x2v = - x2v;
  317.                 y2v += (rand() % 5) - 2;
  318.                 }
  319.  
  320.             if (y2 >= config.mode.disp_vres)
  321.                 {
  322.                 y2 = 2*config.mode.disp_vres - y2 - 2;
  323.                 y2v = - y2v;
  324.                 x2v += (rand() % 5) - 2;
  325.                 }
  326.  
  327.             Move(my_rport,x1,y1);
  328.  
  329.             if (gfx_mode==GSP_GFX)
  330.                 GfxDraw(my_rport,x2,y2);
  331.             else
  332.                 Draw(my_rport,x2,y2);
  333.         }
  334.     }while(!out);
  335.     line_busy = FALSE;
  336.     Wait(0L);
  337. }
  338. /* --------------------------------------------------------------------------------- */
  339.  
  340. /* ------------------------------------------------------------------------------- */
  341.  
  342. void __saveds BoxDemo()
  343. {
  344.     int x,y,w,h,col;
  345.     struct RastPort *rp;
  346.     USHORT areaptrn[] = { 0xf0f0, /*0*/ 0x0f0f, /*1*/ 0xf0f0, 0xf0f0, /*2*/ 0x0f0f, 0x0f0f, 0xf0f0, 0xf0f0,/*3*/
  347.                           0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF /*4*/ };
  348.  
  349.     rp=win->RPort;
  350.     config.mode.disp_hres=(win->Width);
  351.     config.mode.disp_vres=(win->Height);
  352.     config.mode.palet_size=(2<<(win->RPort->BitMap->Depth));
  353.  
  354.     SetDrPt(rp,0xF0F0);
  355.  
  356.     if (fill_mode&PATTERN_F) 
  357.         {SetAfPt(rp,areaptrn,1);}
  358.     else 
  359.         SetAfPt(rp,NULL,1);
  360.  
  361.     rp->Flags&=(~AREAOUTLINE);  // Outline löschen
  362.     box_busy = TRUE;
  363.     do
  364.     {
  365.         w = rand() % (config.mode.disp_hres-1)+1;
  366.         h = rand() % (config.mode.disp_vres-1)+1;
  367.         x = rand() % (config.mode.disp_hres-w);
  368.         y = rand() % (config.mode.disp_vres-h);
  369.         col = rand() % config.mode.palet_size;
  370.  
  371.         SetAPen(rp,col);
  372.  
  373.         if (gfx_mode==GSP_GFX)
  374.             GfxRectFill(rp,x+w,y,x,y+h);
  375.         else
  376.             RectFill(rp,x,y,x+w,y+h);
  377.  
  378.     }while(!out);
  379.     box_busy = FALSE;
  380.     Wait(0L);
  381. }
  382.  
  383. /* ------------------------------------------------------------------------------- */
  384. /* ------------------------------------------------------------------------------- */
  385.  
  386. void __saveds TextDemo(void)
  387. {
  388.     int x,y,col;
  389.     struct RastPort *rp;
  390.  
  391.     rp=win->RPort;
  392.     config.mode.disp_hres=(win->Width);
  393.     config.mode.disp_vres=(win->Height);
  394.     config.mode.palet_size=(2<<(win->RPort->BitMap->Depth));
  395.  
  396.     txt_busy = TRUE;
  397.     do
  398.     {
  399.         x = rand() % (config.mode.disp_hres);
  400.         y = rand() % (config.mode.disp_vres);
  401.         col = rand() % config.mode.palet_size;
  402.  
  403.         SetAPen(rp,col);
  404.  
  405.         Move(rp,x,y);
  406.  
  407.         if (gfx_mode==GSP_GFX)
  408.             GfxText(rp,"Text Demo",9);
  409.         else
  410.             Text(rp,"Text Demo",9);
  411.  
  412.     }while(!out);
  413.     txt_busy = FALSE;
  414.     Wait(0L);
  415. }
  416.  
  417. /* ------------------------------------------------------------------------------- */
  418. /* ------------------------------------------------------------------------------- */
  419.  
  420. void __saveds EllipseDemo()
  421. {
  422.     int x,y,w,h,col;
  423.     struct RastPort *rp;
  424.     USHORT areaptrn[] = { 0xf0f0, /*0*/ 0x0f0f, /*1*/ 0xf0f0, 0xf0f0, /*2*/ 0x0f0f, 0x0f0f, 0xf0f0, 0xf0f0,/*3*/
  425.                           0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF /*4*/ };
  426.  
  427.     rp=win->RPort;
  428.     config.mode.disp_hres=(win->Width);
  429.     config.mode.disp_vres=(win->Height);
  430.     config.mode.palet_size=(2<<(win->RPort->BitMap->Depth));
  431.  
  432.     SetDrPt(rp,0xF0F0);
  433.  
  434.     if (fill_mode&PATTERN_F) 
  435.         {SetAfPt(rp,areaptrn,1);}
  436.     else 
  437.         SetAfPt(rp,NULL,1);
  438.  
  439.     rp->Flags&=(~AREAOUTLINE);  // Outline löschen
  440.     ell_busy = FALSE;
  441.     do
  442.     {
  443.         w = rand() % (config.mode.disp_hres/2);
  444.         h = rand() % (config.mode.disp_vres/2);
  445.         x = rand() % (config.mode.disp_hres);
  446.         y = rand() % (config.mode.disp_vres);
  447.         col = rand() % config.mode.palet_size;
  448.  
  449.         SetAPen(rp,col);
  450.  
  451.         if (fill_mode&FILL_F) 
  452.         {
  453.             if (gfx_mode==GSP_GFX)
  454.                 GfxAreaEllipse(rp,x,y,w,h);
  455.             else
  456.                 AreaEllipse(rp,x,y,w,h);
  457.         }
  458.         else
  459.         {
  460.             if (gfx_mode==GSP_GFX)
  461.                 GfxDrawEllipse(rp,x,y,w,h);
  462.             else
  463.                 DrawEllipse(rp,x,y,w,h);
  464.         }
  465.  
  466.     }while(!out);
  467.     ell_busy = FALSE;
  468.     Wait(0L);
  469. }
  470.  
  471. /* ------------------------------------------------------------------------------- */
  472.